home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / outcode / outcode2.c next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  912 b   |  34 lines

  1. /* Direct calculation of 2D outcodes, C code fragment */
  2.  
  3. /* number of bits in a word */
  4. #define NUMBITS       sizeof(long)*8
  5.  
  6. /* get the integral form of a floating point number */
  7. #define v(x)       *(long *) &(x)
  8.  
  9. /* get the sign bit of an integer as a value 0 or 1 */
  10. #define s(x)       (((unsigned long)(x)) >> (NUMBITS-1))
  11.  
  12. /* get the absolute value of a floating point number in integral form */
  13. #define a(x)       ((x) & ~(1 << (NUMBITS-1)))
  14.  
  15.     /* these values typically would be calculated once and cached */
  16.     ixmin = v(xmin);
  17.     ixmax = v(xmax);
  18.     iymin = v(ymin);
  19.     iymax = v(ymax);
  20.  
  21.     /* get the bits and absolute value */
  22.     ix = v(x); ax = a(ix);
  23.     /* put the sign bit in bit 0 */
  24.     outcode = s(ix | (ax - ixmin));
  25.  
  26.     /* put the sign bit in bit 1 */
  27.     outcode |= s(~ix & (ixmax - ax)) << 1;
  28.  
  29.     /* do the same for y */
  30.     iy = v(y);
  31.     ay = a(iy);
  32.     outcode |= s(iy | (ay - iymin))     << 2;
  33.     outcode |= s(~iy & (iymax - ay)) << 3;
  34.